home *** CD-ROM | disk | FTP | other *** search
- /*
- File: OSAHelpers.c
-
- Contains: Functions to help you when working with the OSA.
-
- Written by: Andy Bachorski
-
- Copyright: Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
-
- You may incorporate this Apple sample source code into your program(s) without
- restriction. This Apple sample source code has been provided "AS IS" and the
- responsibility for its operation is yours. You are not permitted to redistribute
- this Apple sample source code as "Apple sample source code" after having made
- changes. If you're going to re-distribute the source, we require that you make
- it clear in the source that the code was descended from Apple sample source
- code, but that you've made changes.
-
- Change History (most recent first):
- 7/21/1999 Karl Groethe Updated for Metrowerks Codewarror Pro 2.1
- 12/16/98 afb Fixed leak of AEDesc in OSAHUnloadScriptResource.
- Changed call in OSAHUnloadScriptResource to use file
- reference passed in, rather than CurResFile().
-
- */
-
-
- // Conditionals to setup the build environment the way we like it.
- #include "PrivateConditionals.h"
-
-
- //************** Universal Headers ********************************
-
- #include <AppleScript.h>
- #include <ASDebugging.h>
- #include <OSA.h>
- #include <OSAGeneric.h>
- #include <Resources.h>
-
-
- //************** ANSI Headers ********************************
-
- #include <string.h>
-
-
- //************** Project Headers ********************************
-
- #include "AEHelpers.h"
- #include "OSAHelpers.h"
-
-
- //********** Private Definitions ****************************************
-
- #define kFlagNotSet -1
-
-
- //*************************************************************************
-
- pascal Boolean OSAHAppleScriptIsPresent( void )
- {
- OSErr anErr = noErr;
-
- static long gHasAppleScript = kFlagNotSet;
-
- if ( gHasAppleScript == kFlagNotSet )
- {
- if ( GestaltAvailable() )
- {
- long response;
-
- if ( Gestalt( gestaltAppleScriptAttr, &response ) == noErr )
- {
- gHasAppleScript = ( response & (1L << gestaltAppleScriptPresent) ) != 0;
- }
- }
- else
- {
- gHasAppleScript = false;
- }
- }
-
- return gHasAppleScript;
- }//end HasAppleEvents
-
- //*************************************************************************
-
- pascal OSErr OSAHOpenGenericScriptingComponent( ComponentInstance *compInstPtr )
- {
- OSErr anErr = noErr;
-
- if ( HasAppleEvents() )
- {
- *compInstPtr = OpenDefaultComponent( kOSAComponentType, kOSAGenericScriptingComponentSubtype );
- if ( (*compInstPtr == (ComponentInstance)badComponentInstance)
- || (*compInstPtr == (ComponentInstance)badComponentSelector))
- {
- *compInstPtr = 0; // Set to 0 if no component is found.
- anErr = errOSACantOpenComponent;
- }
- }
- else
- {
- anErr = errOSACantOpenComponent;
- }
-
- return ( anErr );
- }//end OSAHOpenGenericScriptingComponent
-
- //*************************************************************************
-
- pascal OSErr OSAHOpenAppleScriptComponent( ComponentInstance *compInstPtr )
- {
- OSErr anErr = noErr;
-
- if ( HasAppleEvents() )
- {
- *compInstPtr = OpenDefaultComponent( kOSAComponentType, kAppleScriptSubtype );
- if ( (*compInstPtr == (ComponentInstance)badComponentInstance)
- || (*compInstPtr == (ComponentInstance)badComponentSelector))
- {
- *compInstPtr = 0; // Set to 0 if no component is found.
- anErr = errOSACantOpenComponent;
- }
- }
- else
- {
- anErr = errOSACantOpenComponent;
- }
-
- return ( anErr );
- }//end OSAHOpenAppleScriptingComponent
-
- //*************************************************************************
-
- pascal OSErr OSAHLoadScriptResource( ComponentInstance componentInstance, short resFileRef, short scriptResID, OSAID *scriptIDPtr )
- {
- Handle scriptHnd = nil;
- OSAError anErr = noErr;
- short originalResFile = CurResFile();
-
- UseResFile( resFileRef );
-
- *scriptIDPtr = kOSANullScript; // Set to null incase it can't be loaded
-
- scriptHnd = Get1Resource( typeScript, scriptResID );
- if ( scriptHnd == nil )
- {
- anErr = resNotFound;
- }
- else
- {
- AEDesc scriptDesc;
-
- scriptDesc.descriptorType = typeOSAGenericStorage;
- scriptDesc.dataHandle = scriptHnd;
- anErr = OSALoad( componentInstance, &scriptDesc, kOSAModeNull, scriptIDPtr );
- ReleaseResource( scriptHnd ); // Once loaded, don't need the script resource any more.
- }
-
- UseResFile( originalResFile );
-
- return anErr;
- }//end OSAHLoadScriptResource
-
- //*************************************************************************
-
- pascal OSErr OSAHUnloadScriptResource( ComponentInstance componentInstance, short resFileRef, short scriptResID, OSAID scriptID )
- {
- OSAError osaErr = noErr;
-
- long scriptIsModified = 0;
- short originalResFile = CurResFile();
-
- UseResFile( resFileRef );
- osaErr = OSAGetScriptInfo( componentInstance, scriptID, kOSAScriptIsModified, &scriptIsModified );
- if ( osaErr == noErr && scriptIsModified > 0 )
- {
- Handle scriptHnd = Get1Resource( typeScript, scriptResID );
- if ( scriptHnd == nil )
- {
- osaErr = resNotFound;
- }
- else
- {
- AEDesc scriptDesc = { typeNull, nil };
-
- osaErr = OSAStore( componentInstance, scriptID, typeOSAGenericStorage, kOSAModeNull, &scriptDesc );
- if ( osaErr == noErr )
- {
- Size scriptSize = GetHandleSize( scriptDesc.dataHandle );
- SetHandleSize( scriptHnd, scriptSize );
- osaErr = MemError();
- if ( osaErr == noErr )
- {
- BlockMoveData( *scriptDesc.dataHandle, *scriptHnd, scriptSize );
- ChangedResource( scriptHnd );
- osaErr = ResError();
- if ( osaErr == noErr )
- {
- UpdateResFile( resFileRef );
- osaErr = ResError();
- }
- }
- (void) AEDisposeDesc( &scriptDesc );
- }
- ReleaseResource( scriptHnd );
- }
- }
- UseResFile( originalResFile );
-
- return osaErr;
- }//end OSAHUnloadScriptResource
-
- //*************************************************************************
-
- pascal OSAError OSAHGetHandlerNames( ComponentInstance compInst, OSAID scriptID, AEDescList* handlerListPtr )
- {
- OSAError anErr = noErr;
- ComponentInstance asCompInst;
-
- anErr = OSAGenericToRealID( compInst, &scriptID, &asCompInst );
- if ( anErr == noErr )
- {
- anErr = OSAGetHandlerNames( asCompInst, kOSANullMode, scriptID, handlerListPtr );
- }
- return anErr;
- }//end OSAHGetHandlerNames
-
- //*************************************************************************
-
- pascal Boolean OSAHHandlerIsInHandlerList( const AEDescList *handlerListPtr, const AEDesc *nameDescPtr )
- {
- OSErr anErr = noErr;
- Boolean hasHandler = false;
-
- SInt32 itemCount;
-
- anErr = AECountItems( handlerListPtr, &itemCount );
- if ( anErr == noErr )
- {
- UInt32 index;
- DescType nameType = nameDescPtr->descriptorType;
-
- for ( index = 1; index <= itemCount; index++ )
- {
- SInt32 actualSize;
- char handlerName[128];
- DescType keyWord;
- DescType actualType;
-
- anErr = AEGetNthPtr( handlerListPtr, index, nameType, &keyWord, &actualType,
- &handlerName, sizeof( handlerName ), &actualSize );
- if ( anErr == noErr && strncmp( handlerName, (char*)&(*nameDescPtr->dataHandle), actualSize ) == 0 )
- {
- hasHandler = true;
- break;
- }
- }
- }
- return hasHandler;
- }//end OSAHHandlerIsInHandlerList
-
- //*************************************************************************
-
- pascal Boolean OSAHScriptHasHandler( ComponentInstance compInst, OSAID scriptID, const AEDesc *nameDescPtr )
- {
- AEDescList handlerList;
- OSErr anErr = noErr;
- Boolean hasHandler = false;
-
- anErr = OSAHGetHandlerNames( compInst, scriptID, &handlerList );
- if ( anErr == noErr )
- {
- hasHandler = OSAHHandlerIsInHandlerList( &handlerList, nameDescPtr );
- }
- AEDisposeDesc( &handlerList );
-
- return hasHandler;
- }//end OSAHScriptHasHandler
-
- //*************************************************************************
-
-